package com.atguigu.web.admin.controller.system;


import com.atguigu.common.result.Result;
import com.atguigu.model.entity.SystemUser;
import com.atguigu.model.enums.BaseStatus;
import com.atguigu.web.admin.service.SystemUserService;
import com.atguigu.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {
    @Autowired
    SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
//        启动针对SystemUser实体类的分页插件，没有条件只有列表分页，所以不用加Lambda
        IPage<SystemUser> page = new Page<>(current,size);
//        启动service的根据前端传的QueryVo和我们page插件设置的需求的查询方法，返回的SystemUserItemVo
//        数据实体类是不包含SystemUser中敏感属性字段的
        IPage<SystemUserItemVo> systemUserItemVoIPage = systemUserService.pageSystemUserByQuery(page,queryVo);
        return Result.ok(systemUserItemVoIPage);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = systemUserService.getSystemUserById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        if (systemUser.getPassword() != null){
//            把前端传来的密码加密再装进SystemUser实体类中
           systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }else{
//            这里如果是空字符串，不更新密码
            systemUser.setPassword(null);
        }
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUser::getUsername,username);
//        统计系统用户表里有几个符合前台传入的username重名的
        long count = systemUserService.count(queryWrapper);
        return Result.ok(count == 0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> updateWrapper = new LambdaUpdateWrapper<>();
//        先找到系统用户里id和前端输入id相同的
        updateWrapper.eq(SystemUser::getId,id);
//        将该id用户的状态设置为前端传入的状态
        updateWrapper.set(SystemUser::getStatus,status);
        systemUserService.update(updateWrapper);
        return Result.ok();
    }
}
